
import ColumnCreator from './ColumnCreator';
/**
 * 表创建器
 * @author xueyanjun
 */
class TableCreator{
	constructor(name){
		this.name = name;
		this.columns = [];
	}
	
	string(columnName,length){
		return this.text(columnName,length);
	}
	
	text(columnName,length){
		return this.addColumn(columnName,'TEXT',length);
	}
	
	char(columnName,length){
		return this.addColumn(columnName,'CHAR',length);
	}
	
	varchar(columnName,length){
		return this.addColumn(columnName,'VARCHAR',length);
	}
	
	real(){
		return this.addColumn(columnName,'REAL',length);
	}
	
	blob(columnName,length){
		return this.addColumn(columnName,'BLOB',length);
	}
	
	int(columnName,length){
		return this.addColumn(columnName,'INT',length);
	}
	
	integer(columnName,length){
		return this.addColumn(columnName,'INTEGER',length);
	}
	
	tinyint(columnName,length){
		return this.addColumn(columnName,'TINYINT',length);
	}
	
	smallint(columnName,length){
		return this.addColumn(columnName,'SMALLINT',length);
	}
	
	mediumint(columnName,length){
		return this.addColumn(columnName,'MEDIUMINT',length);
	}
	
	bigint(columnName,length){
		return this.addColumn(columnName,'BIGINT',length);
	}
	
	double(columnName,length1=12,length2=2){
		const length = length1 + ',' + length2;
		return this.addColumn(columnName,'DOUBLE',length);
	}
	
	float(columnName,length1=12,length2=2){
		const length = length1 + ',' + length2;
		return this.addColumn(columnName,'FLOAT',length);
	}
	
	numeric(columnName,length){
		return this.addColumn(columnName,'NUMERIC',length);
	}
	
	decimal(columnName,length1=12,length2=2){
		const length = length1 + ',' + length2;
		return this.addColumn(columnName,'DECIMAL',length);
	}
	
	boolean(columnName){
		return this.addColumn(columnName,'BOOLEAN');
	}
	
	addColumn(name,type,length){
		const c = new ColumnCreator(name);
		c.type(type,length);
		this.columns.push(c);
		return c;
	}
	
	getSQL(){
		const cols = this.columns;
		let columnText = '';
		for (let i = 0; i < cols.length; i++) {
			let col = cols[i];
			columnText += col.getSQL()
			if(i !== cols.length-1){
				columnText += ',';
			}
		}
		return 'CREATE TABLE IF NOT EXISTS '+ this.name + '(' +columnText+ ')';
	}
}


export default TableCreator;